package com.car.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jms.Session;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.car.service.CarrentalorderformService;
import com.car.service.CartableService;
import com.car.service.CartypetableService;
import com.car.vo.CartableVo;
import com.car.vo.CartypetableVo;
import com.car.entity.Cartable;
import com.car.entity.Detailedlistofrentalcharges;
import com.car.entity.Storetable;
import com.car.entity.User;
import com.car.service.CouponService;
import com.car.service.DetailedlistofrentalchargesService;
import com.car.service.IntegralService;
import com.car.service.LaorderService;
import com.car.service.StoretableService;
import com.car.service.UserService;
import com.car.service.send.OrderTimeoutProducer;
import com.car.util.Fianl;
import com.car.util.Random;

@Controller
@RequestMapping("/jump")
public class jumpController {
	@Autowired
	UserService userService;
	@Autowired
	IntegralService integralService;
	@Autowired
	CartypetableService carType;
	@Autowired
	CouponService coiponService;
	@Autowired
	CartableService cartableService;
	@Autowired
	StoretableService storeService;
	@Autowired
	CarrentalorderformService orderService;
	@Autowired
	DetailedlistofrentalchargesService listService;
	@Autowired
	LaorderService laorder;
	/*
	 * @RequestMapping({"/register",""}) public String register(){
	 * 
	 * 
	 * 
	 * @RequestMapping({ "/index", "" }) public String index() { return "index";
	 * }
	 * 
	 * @RequestMapping({ "/register", "" }) public String register() { return
	 * "carRegister"; }
	 */
	@Autowired
	CarrentalorderformService carrentalorderformService;

	@Autowired
	OrderTimeoutProducer orderTimeoutProducer;

	@RequestMapping({ "/timeout", "" })
	public void loolo() {
		orderTimeoutProducer.sendMessage("sh5802");

	}

	@RequestMapping({ "/homePage", "" })
	public String toHomePage() {
		return "homePage";
	}

	@RequestMapping({ "/toMap", "" })
	public String toMap(@RequestParam("place") String place,
			@RequestParam(value = "zctype", required = false) String zctype,
			@RequestParam(value = "smtype", required = false) String smtype,
			@RequestParam(value = "pan", required = false) String pan,
			@RequestParam(value = "id", required = false) String id,
			@RequestParam(value = "money", required = false) String money,
			@RequestParam(value = "startTime", required = false) String startTime,
			@RequestParam(value = "stopTime", required = false) String stopTime,
			@RequestParam(value = "scsm", required = false) String scsm,
			@RequestParam(value = "qcsm", required = false) String qcsm,
			@RequestParam(value="ccct",required=false) String ccct,
			@RequestParam(value="pageIndex",required=false) String pageIndex,Model model, HttpSession session) {
		if (scsm != null && scsm != "") {
			session.setAttribute("scsm", scsm);
		}
		if (qcsm != null && qcsm != "") {
			session.setAttribute("qcsm", qcsm);
		}
	
		if(startTime!=null && startTime!=""){
			session.setAttribute("startTime", startTime);
			session.setAttribute("stopTime", stopTime);
		}
		if (zctype != null && zctype != "") {
			session.setAttribute("zctype", zctype);
		}
		if (smtype != null && smtype != "") {
			session.setAttribute("smtype", smtype);
		}
		String type = (String) session.getAttribute("zctype");
		if (type.equals("1")) {
			session.setAttribute("myCity", place);
		} else {
			session.setAttribute("hcmycity", place);
		}
		String ss = place.trim();
		session.setAttribute("place", ss);
		model.addAttribute("place", ss);
		model.addAttribute("id", id);
		model.addAttribute("ccct", ccct);
		model.addAttribute("pageIndex", pageIndex);
		model.addAttribute("money", money);
		model.addAttribute("pan", pan);
		return "H5phone/map";
	}

	@RequestMapping({ "/daodianquhuan", "" })
	public String topickUp(@RequestParam(value = "qccity", required = false) String qccity,
			@RequestParam(value = "hccity", required = false) String hccity,
			@RequestParam(value = "zctype", required = false) String zctype,
			@RequestParam(value = "qcsm", required = false) String qcsm,
			@RequestParam(value = "scsm", required = false) String scsm,
			@RequestParam(value = "qccityDetail", required = false) String qccityDetail,
			@RequestParam(value = "hccityDetail", required = false) String hccityDetail, 
			@RequestParam(value = "tc", required = false,defaultValue="0") String tc,
			Model model,
			HttpSession session) {
		if(tc!=null&&tc!=""){
			session.setAttribute("tc",tc);
		}
		if (qcsm != null && qcsm != "") {
			session.setAttribute("qcsm", qcsm);
		}
		if (scsm != null && scsm != "") {
			session.setAttribute("scsm", scsm);
		}
		session.setAttribute("qccityDetail", qccityDetail);

		session.setAttribute("hccityDetail", hccityDetail);

		session.setAttribute("qccity", qccity);

		session.setAttribute("hccity", hccity);

		session.setAttribute("zctype", zctype);

		return "H5phone/daodianquhuan";
	}

	@RequestMapping({ "/toCity", "" })
	public String toCity(@RequestParam("city") String city, @RequestParam("zctype") String zctype,
			@RequestParam("smtype") String smtype, Model model,
			@RequestParam(value = "scsm", required = false) String scsm,
			@RequestParam(value = "qcsm", required = false) String qcsm, HttpSession session) {
		if (scsm != null && scsm != "") {
			session.setAttribute("scsm", scsm);
		}
		if (qcsm != null && qcsm != "") {
			session.setAttribute("qcsm", qcsm);
		}
		session.setAttribute("smtype", smtype);
		session.setAttribute("zctype", zctype);
		model.addAttribute("city", city);
		return "H5phone/city";
	}

	@RequestMapping({ "/toPhoneHomePage", "" })
	public String toPhoneHomePage() {
		return "H5phone/homePage";
	}

	/**
	 * 登陆跳转 @Title: register @Description: TODO(这里用一句话描述这个方法的作用) @param @param
	 * request @param @return 参数 @return String 返回类型 @throws
	 */
	@RequestMapping({ "/logins", "" })
	public String login() {
		return "H5phone/login";
	}

	/**
	 * H5注册 @Title: registerss @Description:
	 * TODO(这里用一句话描述这个方法的作用) @param @return 参数 @return String 返回类型 @throws
	 */
	@RequestMapping({ "/H5register", "" })
	public String registerss() {
		return "H5phone/register";
	}

	@RequestMapping({ "/carType", "" })
	public String carType(@RequestParam(value = "qcTime", required = false) String qcTime,
			@RequestParam(value = "hcTime", required = false) String hcTime,
			@RequestParam(value = "val", required = false, defaultValue = "0") int val,
			@RequestParam(value = "minprice", required = false, defaultValue = "-1") double minprice,
			@RequestParam(value = "maxprice", required = false, defaultValue = "-1") double maxprice,
			@RequestParam(value = "carType", required = false, defaultValue = "") String carTypes,
			@RequestParam(value = "brand", required = false, defaultValue = "") String brand,
			@RequestParam(value = "sale", required = false, defaultValue = "0") String sale,
			HttpSession session,
			Model model) {
		if(sale!=null&&!sale.equals("")){
			session.setAttribute("sale", sale);
		}
		if (carTypes.equals("全部车型")) {
			carTypes = "";
		}
		if (brand.equals("全部品牌")) {
			brand = "";
		}
		Map<String, Object> maps = new HashMap<>();
		maps.put("val", val);
		maps.put("minprice", minprice);
		maps.put("maxprice", maxprice);
		maps.put("carType", carTypes);
		maps.put("brand", brand);
		Map<String, Object> mapss = new HashMap<>();
		if (val != 0) {
			if (brand == "" || minprice == -1 || maxprice == -1 || carTypes == "") {
				mapss = (Map<String, Object>) session.getAttribute("maps");

				double min = Double.valueOf((mapss.get("minprice").toString()));
				double max = Double.valueOf((mapss.get("maxprice").toString()));
				String cartype = mapss.get("carType").toString();
				String brands = mapss.get("brand").toString();

				session.setAttribute("val", val);
				List<CartypetableVo> cartypelist = carType.Brandinfo(val, min, max, cartype, brands);
				if (qcTime != null && qcTime != "") {
					session.setAttribute("qcTime", qcTime);
				}
				if (hcTime != null && hcTime != "") {
					session.setAttribute("hcTime", hcTime);
				}
				session.setAttribute("val", val);
				model.addAttribute("carTypelist", cartypelist);
				session.setAttribute("maps", mapss);
				return "H5phone/carType";
			}
		}
		session.setAttribute("maps", maps);
		mapss = (Map<String, Object>) session.getAttribute("maps");
		int i = Integer.parseInt(mapss.get("val").toString());
		double min = Double.valueOf((mapss.get("minprice").toString()));
		double max = Double.valueOf((mapss.get("maxprice").toString()));
		String cartype = mapss.get("carType").toString();
		String brands = mapss.get("brand").toString();

		session.setAttribute("val", val);
		List<CartypetableVo> cartypelist = carType.Brandinfo(i, min, max, cartype, brands);
		if (qcTime != null && qcTime != "") {
			session.setAttribute("qcTime", qcTime);
		}
		if (hcTime != null && hcTime != "") {
			session.setAttribute("hcTime", hcTime);
		}
		session.setAttribute("val", val);
		model.addAttribute("carTypelist", cartypelist);
		session.setAttribute("maps", mapss);
		return "H5phone/carType";
	}

	@RequestMapping({ "/tochooseService", "" })
	public String tochoose(){
		return "/H5phone/chooseService";
	}
	@ResponseBody
	@RequestMapping({ "/chooseService", "" })
	public boolean chooseService(CartableVo cartable, HttpSession session) {
		Date qctime = new Date();
		String qcDate;
		String qcTime;
		Date hctime = new Date();
		String hcDate;
		String hcTime;
		Map<String, String> time = new HashMap<>();
		try {

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			qctime = sdf.parse(session.getAttribute("qcTime").toString());
			if (qctime.getHours() > 12) {
				qcTime = "下午" + qctime.getHours() + ":" + qctime.getMinutes();
				qcDate = (qctime.getMonth() + 1) + "月" + qctime.getDate() + "日";
			} else {
				qcTime = "上午" + qctime.getHours() + ":" + qctime.getMinutes();
				qcDate = (qctime.getMonth() + 1) + "月" + qctime.getDate() + "日";
			}
			time.put("qctime", qcTime);
			time.put("qcDate", qcDate);
			hctime = sdf.parse(session.getAttribute("hcTime").toString());
			if (hctime.getHours() > 12) {
				hcTime = "下午" + hctime.getHours() + ":" + hctime.getMinutes();
				hcDate = (hctime.getMonth() + 1) + "月" + hctime.getDate() + "日";
			} else {
				hcTime = "上午" + hctime.getHours() + ":" + hctime.getMinutes();
				hcDate = (hctime.getMonth() + 1) + "月" + hctime.getDate() + "日";
			}
			time.put("hctime", hcTime);
			time.put("hcDate", hcDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		session.setAttribute("times", time);
		String qccity = session.getAttribute("qccity").toString();
		if (qccity.substring(qccity.indexOf("市") + 1).equals("")) {
			Storetable store = storeService.selByCityName(qccity);
			qccity = store.getStoreName();
		}
		List<CartableVo> carinfo = cartableService.selByBrandId(cartable, qccity);
		if(carinfo.size()<1){
			return false;
		}
		int random = Random.listRandom(carinfo.size());
		if (random - 1 < 0) {
			session.setAttribute("carinfos", new CartableVo());
		}
		session.setAttribute("carinfos", carinfo.get(random - 1));
		int timeout = (int) ((hctime.getTime() - qctime.getTime()) / (1000 * 3600 * 1) % 24);
		int count = (int) ((hctime.getTime() - qctime.getTime()) / (1000 * 3600 * 24));
		if (timeout >= 4) {
			count += 1;
		}
		CartableVo cars=(CartableVo)session.getAttribute("carinfos");
		cars.setDayCount(count);
		session.setAttribute("carinfos", cars);
		return true;
	}

	@RequestMapping({ "/orders", "" })
	public String orders(Detailedlistofrentalcharges detail, HttpSession session, Model model) {
		session.setAttribute("detail", detail);
		return "H5phone/orders";
	}

	@RequestMapping({ "/order", "" })
	public String order() {
		return "H5phone/order";
	}

	/**
	 * 个人中心 @Title: center @Description: TODO(这里用一句话描述这个方法的作用) @param @return
	 * 参数 @return String 返回类型 @throws
	 */
	@RequestMapping({ "/persenter", "" })
	public String center(HttpSession session, Model model) {
		Fianl fina = new Fianl();
		User user = (User) session.getAttribute("loginSuccess");
		if (user == null) {
			return "H5phone/login";
		}
		
		User users = userService.selectByPrimaryKey(user.getUserId());
		Integer nums = integralService.sumIntegral(users.getUserId()); // 总共积分
		int counts = carrentalorderformService.selectAllbyuserIdToCount(users.getUserId(), 0);
		int countShun = carrentalorderformService.selectAllbyuserIdToCountShun(users.getUserId(), 0);

		if (nums > fina.getLevelPicYinka()) {
			users.setVipId(2);
		}
		if (nums >= fina.getLevelPicJinka()) {

			users.setVipId(3);
		}

		if (nums >= fina.getLevelPicBaijinka()) {

			users.setVipId(4);
		}

		if (nums >= fina.getLevelPicZuanshi()) {

			users.setVipId(5);
		}
		int updVipId = userService.updVip(users.getVipId(), users.getUserId());
		int count = coiponService.countByUserId(user.getUserId());
		int lacount=laorder.count(user.getUserId());
		model.addAttribute("user", users);
		model.addAttribute("lacount",lacount);
		model.addAttribute("count", count);
		model.addAttribute("counts", counts);
		model.addAttribute("countShun", countShun);

		return "H5phone/personalCenter";
	}

	/**
	 * 身份认证 @Title: identityAutos @Description:
	 * TODO(这里用一句话描述这个方法的作用) @param @return 参数 @return String 返回类型 @throws
	 */

	@RequestMapping({ "/identityAuto", "" })
	public String identityAutos() {
		return "H5phone/identityAuthentication";
	}

	/**
	 * 身份证图片跳转 @Title: idCar @Description: TODO(这里用一句话描述这个方法的作用) @param @return
	 * 参数 @return String 返回类型 @throws
	 */
	@RequestMapping({ "/idCards", "" })
	public String idCar(@RequestParam(value = "userName", required = false) String userName,
			@RequestParam(value = "userCid", required = false) String userCid, Model model) {
		model.addAttribute("userName", userName);
		model.addAttribute("userCid", userCid);
		return "H5phone/IdCard";
	}

	/**
	 * 密码设置页面 @Title: paspage @Description: TODO(这里用一句话描述这个方法的作用) @param @return
	 * 参数 @return String 返回类型 @throws
	 */
	@RequestMapping({ "/passpages", "" })
	public String paspage() {
		return "H5phone/passpage";
	}

	/**
	 * 驾照认证 @Title: licensess @Description: TODO(这里用一句话描述这个方法的作用) @param @return
	 * 参数 @return String 返回类型 @throws
	 */
	@RequestMapping({ "/licenses", "" })
	public String licensess() {
		return "H5phone/license";
	}

	/**
	 * 积分规则跳转 @Title: intrDetails @Description:
	 * TODO(这里用一句话描述这个方法的作用) @param @return 参数 @return String 返回类型 @throws
	 */
	@RequestMapping({ "/inteDeteails", "" })
	public String intrDetails() {
		return "H5phone/integralDatails";
	}

	/**
	 * 登陆退出 @Title: logouts @Description: TODO(这里用一句话描述这个方法的作用) @param @param
	 * session @param @return 参数 @return String 返回类型 @throws
	 */
	@RequestMapping({ "logout", "" })
	public String logouts(HttpSession session) {
		session.removeAttribute("loginSuccess");
		return "H5phone/login";
	}

	@RequestMapping({ "/screen", "" })
	public String screen() {
		return "H5phone/screen";
	}

}
